const HOST_ROOT = "https://bbs.uestc.edu.cn/mobcent/app/web/index.php";

var accessSecret = "";
var accessToken = "";

function request(path, param, callback, showLoading = true, method = "POST") {
	if (accessSecret.length > 0 && accessToken.length > 0) {
		beginRequest(path, param, callback, showLoading, method);
	} else {
		uni.getStorage({
			key: 'uni_user_data',
			success: function(res) {
				var data = res.data;
				setAccessSecret(data.secret);
				setAccessToken(data.token);
				beginRequest(path, param, callback, showLoading, method);
			},
			fail: function() {
				beginRequest(path, param, callback, showLoading, method);
			}
		});

	}

}

function beginRequest(path, param, callback, showLoading = true, method = "POST") {
	var parameters = {};
	for (var key in param) {
		parameters[key] = param[key];
	}
	parameters["r"] = path;
	if (accessSecret.length > 0 && accessToken.length > 0) {
		parameters["accessSecret"] = accessSecret;
		parameters["accessToken"] = accessToken;
	}

	if (showLoading) {
		uni.showLoading({
			title: "加载中.."
		})
	}
	uni.request({
		url: HOST_ROOT,
		data: parameters,
		header: {
			'Content-Type': 'application/x-www-form-urlencoded'
		},
		method: method,
		success(respondData) {
			if (showLoading) {
				uni.hideLoading();
			}
			var code = respondData.data.rs;
			if (code == 1) {
				if (callback.success) {
					callback.success(respondData.data);
				}

			} else {
				if (callback.fail) {
					callback.fail(respondData.data.errcode);
				}
			}

		},
		fail(res) {
			if (showLoading) {
				uni.hideLoading();
				uni.showToast({
					icon:"none",
					title:"网络错误"
				})
			}
			if (callback.fail) {
				callback.fail("网络错误");
			}
			
		},
		complete() {

		}
	})
}

function uploadFile(filePath,callback,fileType = "image"){
	uni.showLoading({
		title: "上传中.."
	})
	let url = HOST_ROOT+"?accessSecret="+accessSecret+"&accessToken="+accessToken+"&module=forum&r=forum%2Fsendattachmentex&type=image"
	console.log(url)
	uni.uploadFile({
		filePath:filePath,
		fileType:fileType,
		name:"uploadFile[]",
		filename:"upload_0.jpg",
		url:url,
		success(res){
			let data = JSON.parse(res.data)
			console.log(data)
			if(data.rs == 1 && data.body.attachment.length > 0){
				if (callback.success) {
					callback.success(data.body.attachment[0]);
				}
			}else{
				uni.showToast({
					icon:"none",
					title:"上传失败"
				})
				if (callback.fail) {
					callback.fail("上传失败");
				}
			}
			
		},
		fail(err){
			console.log(err)
			if (callback.fail) {
				callback.fail("网络错误");
			}
			uni.showToast({
				icon:"none",
				title:"网络错误"
			})
		},
		complete() {
			uni.hideLoading()
			
		}
	})
}

function setAccessSecret(key) {
	accessSecret = key;

}

function setAccessToken(key) {
	accessToken = key;
}

function checkMsgText(msgText,callback){
	if(msgText == null || msgText.length == 0){
		if(callback.success){
			callback.success()
		}
		return
	}
	uni.showLoading({
		title:"检测中..."
	})
	wx.cloud.callFunction({
		name: 'checkMsg',
		data: {
			"action": "msgSecCheck",
			"content": msgText
		}
	}).then(res => {
		uni.hideLoading()
		if (res.result.code == 200) {
			if(callback.success){
				callback.success()
			}
		} else {
			uni.showToast({
				icon: "none",
				title: res.result.msg
			})
			if(callback.fail){
				callback.fail()
			}
		}
	})
}


export default {
	request,
	uploadFile,
	checkMsgText,
	setAccessSecret,
	setAccessToken
}
